package com.cw.jeeyt.common.process;

import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cw.lang.cache.Cache;

/**
 * 流程定义缓存
 * * @author liuruijun
 *
 * @version 2017-12-05
 */
@Component
public class ProcessDefCache {

    private static final String ACT_CACHE_PD_ID_ = "pd_id_";
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private Cache<String, ProcessDef> cache;

    /**
     * 获得流程定义对象
     *
     * @param procDefId
     * @return
     */
    public ProcessDef get(String procDefId) {

        ProcessDef processDef = cache.get(ACT_CACHE_PD_ID_ + procDefId);
        if (processDef != null) {
            return processDef;
        }
        ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
            .processDefinitionId(procDefId)
            .singleResult();
        if (pd != null) {
            processDef = ProcessDef.builder()
                .id(pd.getId())
                .name(pd.getName())
                .key(pd.getKey())
                .version(pd.getVersion())
                .build();
            cache.set(ACT_CACHE_PD_ID_ + procDefId, processDef);
        }
        return processDef;
    }

}
